<template>
  <!-- 面包屑导航区域 -->
  <breadcrumb :childMenu="['商品管理', '商品列表']"></breadcrumb>
  <!-- 卡片视图区域 -->
  <el-card>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input
          placeholder="请输入内容"
          v-model="queryInfo.searchTerm"
          clearable
          @clear="getGoodsList"
        >
          <template #append>
            <el-button icon="el-icon-search" @click="getGoodsList"></el-button>
          </template>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="goAddPage"> 添加商品</el-button>
      </el-col>
    </el-row>
    <el-table :data="goodsList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column
        label="商品名称"
        prop="goodsName"
        width="200px"
      ></el-table-column>
      <el-table-column
        label="商品价格(元)"
        prop="goodsPrice"
        width="95px"
      ></el-table-column>
      <el-table-column
        label="商品重量"
        prop="goodsWeight"
        width="70px"
      ></el-table-column>
      <el-table-column
        label="商品数量"
        prop="goodsNumber"
        width="70px"
      ></el-table-column>
      <el-table-column label="创建时间" prop="crteTime" width="180px">
        <template v-slot="props">
          {{ this.$filters.dateFormat(props.row.crteTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120px">
        <template v-slot="props">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeById(props.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      :pageInfo="pageInfo"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    ></el-pagination>
  </el-card>
</template>

<script>
import Breadcrumb from "@/common/Breadcrumb.vue";
import ElPagination from "@/common/ElPagination.vue";
import { getGoodsList, removeById } from "network/goods.js";
export default {
  components: {
    Breadcrumb,
    ElPagination
  },
  data() {
    return {
      queryInfo: {
        searchTerm: "",
        pageIndex: 1,
        pageSize: 5
      },
      //商品列表
      goodsList: [],
      //分页信息
      pageInfo: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const res = await getGoodsList(this.queryInfo);
      if (!res.success)
        return this.$message.error({ showClose: true, message: res.msg });
      this.goodsList = res.response;
      this.pageInfo.totalCount = res.totalCount;
      this.pageInfo.pageIndex = res.pageIndex;
      this.pageInfo.pageSize = res.pageSize;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newIndex) {
      this.queryInfo.pageIndex = newIndex;
      this.getGoodsList();
    },
    async removeById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该商品,是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (confirmResult !== "confirm") return this.$message.info("已取消删除");

      const res = await removeById(id);
      if (!res.success)
        return this.$message.error({ showClose: true, message: res.msg });
      this.$message.success({ showClose: true, message: res.msg });
      this.getGoodsList();
    },
    //跳转到添加页面
    goAddPage() {
      this.$router.push("/goods/addGoods");
    }
  }
};
</script>

<style lang="less" scoped></style>
